#include "point.h"

point::point()
{
	x = y = z = 0.0f;
}
point::point(double XYZ)
{
	x = y = z = XYZ;
}
point::point(double X, double Y, double Z)
{
	x = X;
	y = Y;
	z = Z;
}
point::~point(){}
void point::getPointFromXML(std::ifstream &file)
{
	char line[MAX_LENGTH];
	file.getline(line, MAX_LENGTH);
	std::string nazwaWezla;
	std::string temp(line);

	unsigned int i(0);

	while(temp[i] == 9 || temp[i] == 32)
		++i;

	++i;
	while(temp[i] != '>')
	{
		nazwaWezla.push_back(temp[i]);
		++i;
	}
	while (nazwaWezla!="/point")
	{
		file.getline(line, MAX_LENGTH);
		temp.clear();
		temp.append(line);
		nazwaWezla.clear();

		i=0;
		while(temp[i] == 9 || temp[i] == 32)
			++i;
		if(temp[i] == '<')
		{
			
			std::string nazwaWezlaKoniec;
			std::string zawartoscWezla;
			++i;
			while(temp[i] != '>')
			{
				nazwaWezla.push_back(temp[i]);
				++i;
			}
			if(nazwaWezla == "X")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/X")
					this->x = atof(zawartoscWezla.c_str());
				continue;
			}
			else if(nazwaWezla == "Y")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/Y")
					this->y = atof(zawartoscWezla.c_str());
				continue;
			}
			else if(nazwaWezla == "Z")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/Z")
					this->z = atof(zawartoscWezla.c_str());
				continue;
			}
		}
	}
}
void point::normalize()
{
	double temp(this->width());
	if (temp!=0.0)
	{
		this->x /= temp;
		this->y /= temp;
		this->z /= temp;
	}
}
double point::width()
{
	return sqrt((this->x * this->x) + (this->y * this->y) + (this->z * this->z));
}
void point::set(double X, double Y, double Z)
{
	this->x = X;
	this->y = Y;
	this->z = Z;
}
point point::move(double X, double Y, double Z)
{
	point temp;
	temp.x = this->x + X;
	temp.y = this->y + Y;
	temp.z = this->z + Z;
	return temp;
}
point & point::operator=( const point &other )
{
	this->x = other.x;
	this->y = other.y;
	this->z = other.z;
	return *this; 
}
point point::operator-( point &other )
{
	return point(this->x - other.x, this->y - other.y, this->z - other.z);
}
point & point::operator-=( point &other )
{
	this->x -= other.x;
	this->y -= other.y;
	this->z	-= other.z;
	return *this;
}
point point::operator+( point &other )
{
	return point(this->x + other.x, this->y + other.y, this->z + other.z);
}
point & point::operator+=( point &other )
{
	this->x += other.x;
	this->y += other.y;
	this->z	+= other.z;
	return *this;
}
point point::operator*( double &scalar )
{
	point temp;
	temp.x = this->x * scalar;
	temp.y = this->y * scalar;
	temp.z = this->z * scalar;
	return temp;
}
bool point::operator==( const point &other )
{
	if ( (this->x == other.x) && (this->y == other.y) && (this->z == other.z))
		return true;
	else 
		return false;
}
bool point::operator!=( point &other )
{
	if ( (this->x == other.x) && (this->y == other.y) && (this->z == other.z))
		return false;
	else 
		return true;
}